package com.example.wanshengtv;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
import androidx.lifecycle.ViewModelProvider;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.bin.david.form.data.table.TableData;
import com.example.wanshengtv.bean.CGDongTai;
import com.example.wanshengtv.bean.CHDongTai;
import com.example.wanshengtv.bean.KCDongTai;
import com.example.wanshengtv.bean.PZDongTai;
import com.example.wanshengtv.bean.SCDongTai;
import com.example.wanshengtv.bean.TVBaoGong;
import com.example.wanshengtv.bean.XSDongTai;
import com.example.wanshengtv.databinding.ActivityYunYingBinding;
import com.example.wanshengtv.http.api.CGDongTaiAPI;
import com.example.wanshengtv.http.api.CHDongTaiAPI;
import com.example.wanshengtv.http.api.KCDongTaiAPI;
import com.example.wanshengtv.http.api.PZDongTaiAPI;
import com.example.wanshengtv.http.api.SCDongTaiAPI;
import com.example.wanshengtv.http.api.TVBaoGongApi;
import com.example.wanshengtv.http.api.XSDongTaiAPI;
import com.example.wanshengtv.http.model.HttpData;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.http.listener.OnHttpListener;
import com.hjq.toast.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class YunYingActivity extends Activity implements LifecycleOwner, OnHttpListener {
    CompositeDisposable compositeDisposable = new CompositeDisposable();
    private final LifecycleRegistry mLifecycle = new LifecycleRegistry(this);
    ActivityYunYingBinding binding;
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd EEEE HH:mm", Locale.CHINA);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yun_ying);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_yun_ying);
        binding.setLifecycleOwner(this);

        df.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        binding.time.setText(df.format(System.currentTimeMillis()));
        binding.setXSDongTai(new XSDongTai());
        binding.setCGDongTai(new CGDongTai());
        binding.setKCDongTai(new KCDongTai());
        binding.setSCDongTai(new SCDongTai());
        binding.setPZDongTai(new PZDongTai());
        binding.setCHDongTai(new CHDongTai());
    }

    //采购
    private void loopSequenceCG() {
        Disposable disposable = getDataFromCG()
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        Log.d("lx", "loopSequence subscribe");
                    }
                })
                .doOnNext(new Consumer<HttpData<CGDongTai>>() {
                    @Override
                    public void accept(HttpData<CGDongTai> integer) throws Exception {
                        Log.d("lx", "loopSequence doOnNext: " + integer);
                    }
                })
                .doOnError(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.show(throwable.getMessage());
                    }
                })
                .delay(60, TimeUnit.SECONDS, true)       // 设置delayError为true，表示出现错误的时候也需要延迟5s进行通知，达到无论是请求正常还是请求失败，都是5s后重新订阅，即重新请求。
                .subscribeOn(Schedulers.io())
                .repeat()   // repeat保证请求成功后能够重新订阅。
                .retry()    // retry保证请求失败后能重新订阅
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<HttpData<CGDongTai>>() {
                    @Override
                    public void accept(HttpData<CGDongTai> integer) throws Exception {
                        //更新视图
                        Log.d("setCGDongTai", integer.getData().toString());
                        binding.setCGDongTai(integer.getData());
                        binding.time.setText(df.format(System.currentTimeMillis()));
                        //  cgDongTai = integer.getData();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.show(throwable.getMessage());
                        binding.time.setText(df.format(System.currentTimeMillis()));
                    }
                });
        compositeDisposable.add(disposable);
    }

    //采购动态
    private Observable<HttpData<CGDongTai>> getDataFromCG() {
        return Observable.create(new ObservableOnSubscribe<HttpData<CGDongTai>>() {
            @Override
            public void subscribe(ObservableEmitter<HttpData<CGDongTai>> emitter) throws Exception {
                if (emitter.isDisposed()) {
                    return;
                }
                EasyHttp.get(YunYingActivity.this)
                        .api(new CGDongTaiAPI())
                        .request(new HttpCallback<HttpData<CGDongTai>>(YunYingActivity.this) {
                            @Override
                            public void onSucceed(HttpData<CGDongTai> result) {
                                //table.setData(result.getData());
                                emitter.onNext(result);
                                emitter.onComplete();
                            }

                            @Override
                            public void onFail(Exception e) {
                                emitter.onError(new Exception("连接网络失败" + e.getMessage()));
                                return;
                            }
                        });


            }
        });
    }

    //销售动态
    private void loopSequenceXS() {
        Disposable disposable = getDataFromXS()
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        Log.d("lx", "loopSequence subscribe");
                    }
                })
                .doOnNext(new Consumer<HttpData<XSDongTai>>() {
                    @Override
                    public void accept(HttpData<XSDongTai> integer) throws Exception {
                        Log.d("lx", "loopSequence doOnNext: " + integer);
                    }
                })
                .doOnError(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.show(throwable.getMessage());
                    }
                })
                .delay(60, TimeUnit.SECONDS, true)       // 设置delayError为true，表示出现错误的时候也需要延迟5s进行通知，达到无论是请求正常还是请求失败，都是5s后重新订阅，即重新请求。
                .subscribeOn(Schedulers.io())
                .repeat()   // repeat保证请求成功后能够重新订阅。
                .retry()    // retry保证请求失败后能重新订阅
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<HttpData<XSDongTai>>() {
                    @Override
                    public void accept(HttpData<XSDongTai> integer) throws Exception {
                        //更新视图
                        Log.d("setCGDongTai", integer.getData().toString());
                        binding.setXSDongTai(integer.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.show(throwable.getMessage());

                    }
                });
        compositeDisposable.add(disposable);
    }

    //销售态
    private Observable<HttpData<XSDongTai>> getDataFromXS() {
        return Observable.create(new ObservableOnSubscribe<HttpData<XSDongTai>>() {
            @Override
            public void subscribe(ObservableEmitter<HttpData<XSDongTai>> emitter) throws Exception {
                if (emitter.isDisposed()) {
                    return;
                }
                EasyHttp.get(YunYingActivity.this)
                        .api(new XSDongTaiAPI())
                        .request(new HttpCallback<HttpData<XSDongTai>>(YunYingActivity.this) {
                            @Override
                            public void onSucceed(HttpData<XSDongTai> result) {
                                //table.setData(result.getData());
                                emitter.onNext(result);
                                emitter.onComplete();
                            }

                            @Override
                            public void onFail(Exception e) {
                                emitter.onError(new Exception("连接网络失败" + e.getMessage()));
                                return;
                            }
                        });


            }
        });
    }

    //库存动态
    private void loopSequenceKC() {
        Disposable disposable = getDataFromKC()
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        Log.d("lx", "loopSequence subscribe");
                    }
                })
                .doOnNext(new Consumer<HttpData<KCDongTai>>() {
                    @Override
                    public void accept(HttpData<KCDongTai> integer) throws Exception {
                        Log.d("lx", "loopSequence doOnNext: " + integer);
                    }
                })
                .doOnError(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.show(throwable.getMessage());
                    }
                })
                .delay(60, TimeUnit.SECONDS, true)       // 设置delayError为true，表示出现错误的时候也需要延迟5s进行通知，达到无论是请求正常还是请求失败，都是5s后重新订阅，即重新请求。
                .subscribeOn(Schedulers.io())
                .repeat()   // repeat保证请求成功后能够重新订阅。
                .retry()    // retry保证请求失败后能重新订阅
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<HttpData<KCDongTai>>() {
                    @Override
                    public void accept(HttpData<KCDongTai> integer) throws Exception {
                        //更新视图
                        Log.d("setCGDongTai", integer.getData().toString());
                        binding.setKCDongTai(integer.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.show(throwable.getMessage());

                    }
                });
        compositeDisposable.add(disposable);
    }

    //库存动态
    private Observable<HttpData<KCDongTai>> getDataFromKC() {
        return Observable.create(new ObservableOnSubscribe<HttpData<KCDongTai>>() {
            @Override
            public void subscribe(ObservableEmitter<HttpData<KCDongTai>> emitter) throws Exception {
                if (emitter.isDisposed()) {
                    return;
                }
                EasyHttp.get(YunYingActivity.this)
                        .api(new KCDongTaiAPI())
                        .request(new HttpCallback<HttpData<KCDongTai>>(YunYingActivity.this) {
                            @Override
                            public void onSucceed(HttpData<KCDongTai> result) {
                                //table.setData(result.getData());
                                emitter.onNext(result);
                                emitter.onComplete();
                            }

                            @Override
                            public void onFail(Exception e) {
                                emitter.onError(new Exception("连接网络失败" + e.getMessage()));
                                return;
                            }
                        });


            }
        });
    }

    //生产动态
    private void loopSequenceSC() {
        Disposable disposable = getDataFromSC()
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        Log.d("lx", "loopSequence subscribe");
                    }
                })
                .doOnNext(new Consumer<HttpData<SCDongTai>>() {
                    @Override
                    public void accept(HttpData<SCDongTai> integer) throws Exception {
                        Log.d("lx", "loopSequence doOnNext: " + integer);
                    }
                })
                .doOnError(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.show(throwable.getMessage());
                    }
                })
                .delay(60, TimeUnit.SECONDS, true)       // 设置delayError为true，表示出现错误的时候也需要延迟5s进行通知，达到无论是请求正常还是请求失败，都是5s后重新订阅，即重新请求。
                .subscribeOn(Schedulers.io())
                .repeat()   // repeat保证请求成功后能够重新订阅。
                .retry()    // retry保证请求失败后能重新订阅
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<HttpData<SCDongTai>>() {
                    @Override
                    public void accept(HttpData<SCDongTai> integer) throws Exception {
                        //更新视图
                        Log.d("setCGDongTai", integer.getData().toString());
                        binding.setSCDongTai(integer.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.show(throwable.getMessage());

                    }
                });
        compositeDisposable.add(disposable);
    }

    //生产动态
    private Observable<HttpData<SCDongTai>> getDataFromSC() {
        return Observable.create(new ObservableOnSubscribe<HttpData<SCDongTai>>() {
            @Override
            public void subscribe(ObservableEmitter<HttpData<SCDongTai>> emitter) throws Exception {
                if (emitter.isDisposed()) {
                    return;
                }
                EasyHttp.get(YunYingActivity.this)
                        .api(new SCDongTaiAPI())
                        .request(new HttpCallback<HttpData<SCDongTai>>(YunYingActivity.this) {
                            @Override
                            public void onSucceed(HttpData<SCDongTai> result) {
                                //table.setData(result.getData());
                                emitter.onNext(result);
                                emitter.onComplete();
                            }

                            @Override
                            public void onFail(Exception e) {
                                emitter.onError(new Exception("连接网络失败" + e.getMessage()));
                                return;
                            }
                        });


            }
        });
    }

    //品质动态
    private void loopSequencePZ() {
        Disposable disposable = getDataFromPZ()
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        Log.d("lx", "loopSequence subscribe");
                    }
                })
                .doOnNext(new Consumer<HttpData<PZDongTai>>() {
                    @Override
                    public void accept(HttpData<PZDongTai> integer) throws Exception {
                        Log.d("lx", "loopSequence doOnNext: " + integer);
                    }
                })
                .doOnError(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.show(throwable.getMessage());
                    }
                })
                .delay(60, TimeUnit.SECONDS, true)       // 设置delayError为true，表示出现错误的时候也需要延迟5s进行通知，达到无论是请求正常还是请求失败，都是5s后重新订阅，即重新请求。
                .subscribeOn(Schedulers.io())
                .repeat()   // repeat保证请求成功后能够重新订阅。
                .retry()    // retry保证请求失败后能重新订阅
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<HttpData<PZDongTai>>() {
                    @Override
                    public void accept(HttpData<PZDongTai> integer) throws Exception {
                        //更新视图
                        Log.d("setCGDongTai", integer.getData().toString());
                        binding.setPZDongTai(integer.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.show(throwable.getMessage());

                    }
                });
        compositeDisposable.add(disposable);
    }

    //品质动态
    private Observable<HttpData<PZDongTai>> getDataFromPZ() {
        return Observable.create(new ObservableOnSubscribe<HttpData<PZDongTai>>() {
            @Override
            public void subscribe(ObservableEmitter<HttpData<PZDongTai>> emitter) throws Exception {
                if (emitter.isDisposed()) {
                    return;
                }
                EasyHttp.get(YunYingActivity.this)
                        .api(new PZDongTaiAPI())
                        .request(new HttpCallback<HttpData<PZDongTai>>(YunYingActivity.this) {
                            @Override
                            public void onSucceed(HttpData<PZDongTai> result) {
                                //table.setData(result.getData());
                                emitter.onNext(result);
                                emitter.onComplete();
                            }

                            @Override
                            public void onFail(Exception e) {
                                emitter.onError(new Exception("连接网络失败" + e.getMessage()));
                                return;
                            }
                        });


            }
        });
    }

    //出货动态
    private void loopSequenceCH() {
        Disposable disposable = getDataFromCH()
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        Log.d("lx", "loopSequence subscribe");
                    }
                })
                .doOnNext(new Consumer<HttpData<CHDongTai>>() {
                    @Override
                    public void accept(HttpData<CHDongTai> integer) throws Exception {
                        Log.d("lx", "loopSequence doOnNext: " + integer);
                    }
                })
                .doOnError(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.show(throwable.getMessage());
                    }
                })
                .delay(60, TimeUnit.SECONDS, true)       // 设置delayError为true，表示出现错误的时候也需要延迟5s进行通知，达到无论是请求正常还是请求失败，都是5s后重新订阅，即重新请求。
                .subscribeOn(Schedulers.io())
                .repeat()   // repeat保证请求成功后能够重新订阅。
                .retry()    // retry保证请求失败后能重新订阅
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<HttpData<CHDongTai>>() {
                    @Override
                    public void accept(HttpData<CHDongTai> integer) throws Exception {
                        //更新视图
                        Log.d("setCGDongTai", integer.getData().toString());
                        binding.setCHDongTai(integer.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.show(throwable.getMessage());

                    }
                });
        compositeDisposable.add(disposable);
    }
    //出货动态
    private Observable<HttpData<CHDongTai>> getDataFromCH() {
        return Observable.create(new ObservableOnSubscribe<HttpData<CHDongTai>>() {
            @Override
            public void subscribe(ObservableEmitter<HttpData<CHDongTai>> emitter) throws Exception {
                if (emitter.isDisposed()) {
                    return;
                }
                EasyHttp.get(YunYingActivity.this)
                        .api(new CHDongTaiAPI())
                        .request(new HttpCallback<HttpData<CHDongTai>>(YunYingActivity.this) {
                            @Override
                            public void onSucceed(HttpData<CHDongTai> result) {
                                //table.setData(result.getData());
                                emitter.onNext(result);
                                emitter.onComplete();
                            }

                            @Override
                            public void onFail(Exception e) {
                                emitter.onError(new Exception("连接网络失败" + e.getMessage()));
                                return;
                            }
                        });


            }
        });
    }

    @NonNull
    @Override
    public Lifecycle getLifecycle() {
        return mLifecycle;
    }

    @Override
    public void onSucceed(Object result) {

    }

    @Override
    public void onFail(Exception e) {

    }

    @Override
    protected void onStart() {
        compositeDisposable.dispose();
        compositeDisposable = new CompositeDisposable();
        loopSequenceCG();
        loopSequenceXS();
        loopSequenceKC();
        loopSequenceSC();
        loopSequencePZ();
        loopSequenceCH();;
        super.onStart();
    }

    @Override
    protected void onStop() {
        if (compositeDisposable != null) {
            compositeDisposable.dispose();
        }
        super.onStop();
    }

}
